package com.cndw.kongfu.messagepush;
import java.io.InputStream;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferInputStream;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.codec.Compress;

import flex.messaging.io.amf.Amf3Input;

public class MyClientHandler extends SimpleChannelHandler {

	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
		ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
		ChannelBufferInputStream inputStream = new ChannelBufferInputStream(buffer);
		dealMessage(inputStream);
//		e.getChannel().close();
	}

	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
		System.out.println("Unexpected exception from downstream."
				+ e.getCause());
		e.getChannel().close();
	}
	
	@Override
	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		super.channelClosed(ctx, e);
	}

	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		super.channelConnected(ctx, e);
//		Tencent.sendXX();
	}

	@Override
	public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
		super.channelDisconnected(ctx, e);
		System.out.println("disconnected...");
		MessagePush.pushMessage("Client disconnected...");
//		System.exit(0);
	}

	public ResponseMessage dealMessage(ChannelBufferInputStream inputStream) {
		Amf3Input input = new Amf3Input(SendTest.context);
		InputStream stream = inputStream;
		ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
		int bodySize = 0;
		int compressSize = 0;
		byte bytes[] = new byte[1024];
		ChannelBuffer bodyBuffer = null;
		ResponseMessage message = null;
		try {
			
			bodySize = 0;
			compressSize = 0;
			do {
				int length = stream.read(bytes);
				if (length != -1)
					buffer.writeBytes(bytes, 0, length);
				while (bodySize == 0 && buffer.readableBytes() >= 4) {
					bodySize = buffer.readInt();
					if (bodySize > 1024 && compressSize == 0) {
						compressSize = bodySize;
						bodySize = 0;
					}
				}
			} while (buffer.readableBytes() < bodySize);
			
			if (compressSize > 0) {
				byte cpomressBytes[] = Compress.decompressBytes(buffer.readBytes(bodySize).array());
				bodyBuffer = buffer.factory().getBuffer(cpomressBytes.length);
				bodyBuffer.writeBytes(cpomressBytes);
			} else {
				bodyBuffer = buffer.factory().getBuffer(bodySize);
				bodyBuffer.writeBytes(buffer, bodySize);
			}
			input.setInputStream(new ChannelBufferInputStream(bodyBuffer));
			message = (ResponseMessage) input.readObject();
			bodyBuffer.clear();
			System.out.println(message.m);
			MessagePush.pushMessage(message.m.toString());
			input.close();
//			if (socket != null)
//				socket.close();
//			return message;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return message;
	}
}